package org.example.demo.service.userService.impl;

import org.example.demo.dto.ProductDetail.ProductInfoSimple;
import org.example.demo.dto.ProductShowParam;
import org.example.mbg.model.Product;
import org.example.demo.service.userService.UserHomeService;
import org.example.mbg.mapper.ProductMapper;
import org.example.mbg.model.Product;
import org.example.mbg.model.ProductExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户首页service
 */
@Service
public class UserHomeServiceImpl implements UserHomeService {
    @Autowired
    private ProductMapper productMapper;

    final static Long DEFAULT_RANGE = 5L;

    @Override
    // 获取小程序首页轮播图商品
    public List<ProductShowParam> genBanner() {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andIdLessThan(DEFAULT_RANGE);
        return productMapper.selectByExample(productExample).stream().map(e -> {
            ProductShowParam productShowParam = new ProductShowParam();
            productShowParam.setId(e.getId());
            productShowParam.setPage(e.getPage());
            productShowParam.setTitle(e.getName());
            productShowParam.setImg(e.getPic());
            productShowParam.setSales(e.getSale());
            productShowParam.setPrice(e.getPrice());
            productShowParam.setDiscount(e.getDiscount());
            productShowParam.setServices(e.getServices());
            productShowParam.setCfav(e.getCfav());
            return productShowParam;
        }).collect(Collectors.toList());
    }

    @Override
    // 获取小程序首页某一个类别下的商品
    public List<ProductShowParam> genCate(Long cateId) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andPageEqualTo(cateId);
        return productMapper.selectByExample(productExample).stream().map(e -> {
            ProductShowParam productShowParam = new ProductShowParam();
            productShowParam.setId(e.getId());
            productShowParam.setPage(e.getPage());
            productShowParam.setTitle(e.getName());
            productShowParam.setImg(e.getPic());
            productShowParam.setSales(e.getSale());
            productShowParam.setPrice(e.getDiscount());
            productShowParam.setDiscount(e.getDiscount());
            productShowParam.setServices(e.getServices());
            productShowParam.setCfav(e.getCfav());
            return productShowParam;
        }).collect(Collectors.toList()).subList(0, 20);
    }

    @Override
    public List<Product> commandProduct() {
        return productMapper.selectByExample(new ProductExample());
    }

    @Override
    public List<Product> findProduct(String value) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andNameLike("%%" + value + "%%");
        return productMapper.selectByExample(productExample);
    }


}
